do (angular) ->
  angular.module 'tini'

  .directive 'post', ($parse, req) ->
    (scope, element, attrs) ->
      postFn = $parse attrs.post
      scope.$watch attrs.post, (post) ->
        if post
          archive = $parse('archive') scope

          proc = (post, content) ->
            moreIndex = content.indexOf '<a id=more></a>'
            hasMore = post.hasMore = moreIndex > -1
            post.content = content
            post.intro = if hasMore then content.substr 0, moreIndex else content
            post

          render = (element, post, archive) ->
            if archive && post.hasMore
              post.more = true
              element.html post.intro
            else
              post.more = false
              element.html post.content

          assign = (post) ->
            render element, post, archive
            postFn.assign scope, post

          if post.content
            assign post
          else
            req.post post, (content) ->
              assign proc post, content
